package cn.vcjf.weapp.api.user.controller;


import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.ResponseBody;
import cn.vcjf.weapp.common.web.TableResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import cn.vcjf.weapp.api.user.entity.UserAddress;
import cn.vcjf.weapp.api.user.service.UserAddressService;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-09-25
 */
@Controller
@RequestMapping("/user/userAddress")
public class UserAddressController {
	@Resource
	private UserAddressService userAddressService;

	@RequestMapping("/addUserAddress")
	@ResponseBody
	public TableResult addUserAddress(@Valid UserAddress userAddress, BindingResult result) {

		TableResult.checkParams(result);
		int count = userAddressService.selectCount(userAddress.getQueryWrapper());
		if (count == 0) {
			userAddress.setDef(1);
		}
		userAddress.preInsert();
		boolean b = userAddressService.insert(userAddress);
		return TableResult.Result(b);
	}

	@RequestMapping("/updateUserAddress")
	@ResponseBody
	public TableResult updateUserAddress(UserAddress userAddress) {
		userAddress.preUpdate();
		boolean b = userAddressService.updateById(userAddress);
		return TableResult.Result(b);
	}

	@RequestMapping("/findUserAddressOne")
	@ResponseBody
	public TableResult findUserAddressOne(UserAddress userAddress) {

		EntityWrapper<UserAddress> wrapper = new EntityWrapper<UserAddress>();

		if (userAddress.getId() != null) {
			wrapper.eq("id", userAddress.getId());
		}
		//Todo

		UserAddress result = userAddressService.selectOne(wrapper);

		return TableResult.OKResult(result);
	}

	@RequestMapping("/findUserAddressList")
	@ResponseBody
	public TableResult findUserAddressList(UserAddress userAddress) {

		EntityWrapper<UserAddress> wrapper = new EntityWrapper<UserAddress>();

		wrapper.eq("sysUserId", userAddress.getSysUserId());
		wrapper.eq("userId", userAddress.getUserId());
		wrapper.orderBy("def", true);

		Page<UserAddress> page = userAddress.getPageInfo();

		page = userAddressService.selectPage(page, wrapper);

		return TableResult.OKResult(page);

	}

	@RequestMapping("/deleteUserAddress")
	@ResponseBody
	public TableResult deleteUserAddress(UserAddress userAddress) {
		userAddress.preUpdate();
		boolean b = userAddressService.deleteById(userAddress.getId());
		return TableResult.Result(b);
	}

}
